﻿using MediatR;
using Product.API.Applcation.Command.Product;
using Product.Infrstructre.Impl;

namespace Product.API.Applcation.CommandHandlers.Product
{
    /// <summary>
    /// 产品删除
    /// </summary>
    public class ProductInfoDeleteCommandHandler : IRequestHandler<ProductInfoDeleteCommand, int>
    {
        protected readonly IbaseProductInfoRepository ibaseProductInfoRepository;

        public ProductInfoDeleteCommandHandler(IbaseProductInfoRepository ibaseProductInfoRepository)
        {
            this.ibaseProductInfoRepository = ibaseProductInfoRepository;
        }

        public async Task<int> Handle(ProductInfoDeleteCommand request, CancellationToken cancellationToken)
        {
            var code = 0;
            foreach (var item in request.ProductsID)
            {
                var list = await ibaseProductInfoRepository.GetAllByIdAsync(item);
                if (list == null)
                {
                    code = -1;
                }
                else
                {
                    list.IsDel = true;
                    list.DelName = request.U_Name;
                    list.DelTime = DateTime.Now;
                    await ibaseProductInfoRepository.UpdateAsync(list);
                    code = 1;
                }
            }
            return code;
        }
    }
}
